package com.vegetable.modules.controller.finance;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vegetable.common.utils.R;
import com.vegetable.modules.controller.AbstractController;
import com.vegetable.modules.entity.finance.po.CustomerCollectionAlertRecordQueryPO;
import com.vegetable.modules.entity.finance.vo.CustomerCollectionAlertRecordVO;
import com.vegetable.modules.entity.finance.vo.CustomerOverdueStatisticsVO;
import com.vegetable.modules.entity.finance.vo.OverdueLevelStatisticsVO;
import com.vegetable.modules.service.finance.ICustomerCollectionAlertRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 客户回款预警 - 实时查询超期订单
 * </p>
 *
 * @author vegetable
 * @since 2025-10-24
 */
@Slf4j
@RestController
@RequestMapping("/finance/customerCollectionAlertRecord")
@Api(value = "客户回款超期查询", tags = "客户回款超期查询")
public class CustomerCollectionAlertRecordController extends AbstractController {

    @Autowired
    private ICustomerCollectionAlertRecordService alertRecordService;

    /**
     * 实时查询超期未回款订单列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "实时查询超期未回款订单列表", notes = "根据客户预警设置，实时查询所有超期未回款的销售出库单")
    public R list(@Valid CustomerCollectionAlertRecordQueryPO queryPO) {
        log.info("实时查询超期未回款订单列表：{}", JSON.toJSONString(queryPO));
        IPage<CustomerCollectionAlertRecordVO> page = alertRecordService.queryOverdueOrders(queryPO);
        return R.data(page);
    }

    /**
     * 按逾期等级统计未回款账期生命周期
     */
    @GetMapping("/levelStatistics")
    @ApiOperation(value = "按逾期等级统计", notes = "按逾期天数等级统计未回款账期生命周期，分为5个等级：15天以内、15-29天、30-59天、60-89天、90天以上")
    public R levelStatistics(@Valid CustomerCollectionAlertRecordQueryPO queryPO) {
        log.info("按逾期等级统计未回款账期：{}", JSON.toJSONString(queryPO));
        List<OverdueLevelStatisticsVO> statistics = alertRecordService.queryOverdueLevelStatistics(queryPO);
        return R.data(statistics);
    }

    /**
     * 按逾期等级查询订单明细
     */
    @GetMapping("/levelDetail")
    @ApiOperation(value = "按逾期等级查询订单明细", notes = "查询某个逾期等级下的具体订单列表，支持分页")
    public R levelDetail(@Valid CustomerCollectionAlertRecordQueryPO queryPO) {
        log.info("按逾期等级查询订单明细：{}", JSON.toJSONString(queryPO));
        if (queryPO.getOverdueLevel() == null) {
            return R.fail("逾期等级参数不能为空");
        }
        IPage<CustomerCollectionAlertRecordVO> page = alertRecordService.queryOverdueLevelDetail(queryPO);
        return R.data(page);
    }

    /**
     * 按客户ID查询逾期统计
     */
    @GetMapping("/customerStatistics")
    @ApiOperation(value = "按客户ID查询逾期统计", notes = "分析每个客户的账期情况，包含各逾期等级的订单数和金额统计")
    public R customerStatistics(@Valid CustomerCollectionAlertRecordQueryPO queryPO) {
        log.info("按客户ID查询逾期统计：{}", JSON.toJSONString(queryPO));
        List<CustomerOverdueStatisticsVO> statistics = alertRecordService.queryCustomerOverdueStatistics(queryPO);
        return R.data(statistics);
    }

    /**
     * 按逾期等级查询客户统计（筛选有指定逾期等级订单的客户）
     */
    @GetMapping("/customerStatisticsByLevel")
    @ApiOperation(value = "按逾期等级查询客户统计", notes = "查询有指定逾期等级订单的客户列表，比如查询所有有90天以上逾期订单的客户")
    public R customerStatisticsByLevel(@Valid CustomerCollectionAlertRecordQueryPO queryPO) {
        log.info("按逾期等级查询客户统计：{}", JSON.toJSONString(queryPO));
        if (queryPO.getOverdueLevel() == null) {
            return R.fail("逾期等级参数不能为空");
        }
        List<CustomerOverdueStatisticsVO> statistics = alertRecordService.queryCustomerStatisticsByLevel(queryPO);
        return R.data(statistics);
    }
}
